package com.ruoyi.zsyh.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zsyh.mapper.ZsyhAfterServiceTypeMapper;
import com.ruoyi.zsyh.domain.ZsyhAfterServiceType;
import com.ruoyi.zsyh.service.IZsyhAfterServiceTypeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 售后工单服务分类Service业务层处理
 * 
 * @author yuexl
 * @date 2022-09-01
 */
@Service
public class ZsyhAfterServiceTypeServiceImpl implements IZsyhAfterServiceTypeService 
{
    @Autowired
    private ZsyhAfterServiceTypeMapper zsyhAfterServiceTypeMapper;

    /**
     * 查询售后工单服务分类
     * 
     * @param id 售后工单服务分类ID
     * @return 售后工单服务分类
     */
    @Override
    public ZsyhAfterServiceType selectZsyhAfterServiceTypeById(String id)
    {
        return zsyhAfterServiceTypeMapper.selectZsyhAfterServiceTypeById(id);
    }

    /**
     * 查询售后工单服务分类列表
     * 
     * @param zsyhAfterServiceType 售后工单服务分类
     * @return 售后工单服务分类
     */
    @Override
    public List<ZsyhAfterServiceType> selectZsyhAfterServiceTypeList(ZsyhAfterServiceType zsyhAfterServiceType)
    {
        return zsyhAfterServiceTypeMapper.selectZsyhAfterServiceTypeList(zsyhAfterServiceType);
    }

    /**
     * 新增售后工单服务分类
     * 
     * @param zsyhAfterServiceType 售后工单服务分类
     * @return 结果
     */
    @Override
    public int insertZsyhAfterServiceType(ZsyhAfterServiceType zsyhAfterServiceType)
    {
        zsyhAfterServiceType.setCreateTime(DateUtils.getNowDate());
        return zsyhAfterServiceTypeMapper.insertZsyhAfterServiceType(zsyhAfterServiceType);
    }

    /**
     * 修改售后工单服务分类
     * 
     * @param zsyhAfterServiceType 售后工单服务分类
     * @return 结果
     */
    @Override
    public int updateZsyhAfterServiceType(ZsyhAfterServiceType zsyhAfterServiceType)
    {
        zsyhAfterServiceType.setUpdateTime(DateUtils.getNowDate());
        return zsyhAfterServiceTypeMapper.updateZsyhAfterServiceType(zsyhAfterServiceType);
    }

    /**
     * 删除售后工单服务分类对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZsyhAfterServiceTypeByIds(String ids)
    {
        return zsyhAfterServiceTypeMapper.deleteZsyhAfterServiceTypeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除售后工单服务分类信息
     * 
     * @param id 售后工单服务分类ID
     * @return 结果
     */
    @Override
    public int deleteZsyhAfterServiceTypeById(String id)
    {
        return zsyhAfterServiceTypeMapper.deleteZsyhAfterServiceTypeById(id);
    }

    /**
     * 查询售后工单服务分类树列表
     * 
     * @return 所有售后工单服务分类信息
     */
    @Override
    public List<Ztree> selectZsyhAfterServiceTypeTree()
    {
        List<ZsyhAfterServiceType> zsyhAfterServiceTypeList = zsyhAfterServiceTypeMapper.selectZsyhAfterServiceTypeList(new ZsyhAfterServiceType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ZsyhAfterServiceType zsyhAfterServiceType : zsyhAfterServiceTypeList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(Long.parseLong(zsyhAfterServiceType.getId()));
            ztree.setpId(Long.parseLong(zsyhAfterServiceType.getServiceParentId()));
            ztree.setName(zsyhAfterServiceType.getServiceName());
            ztree.setTitle(zsyhAfterServiceType.getServiceName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public  List<ZsyhAfterServiceType> getServiceTypeTree()
    {

        List<ZsyhAfterServiceType> zsyhAfterServiceTypeList = zsyhAfterServiceTypeMapper.selectZsyhAfterServiceTypeList(new ZsyhAfterServiceType());
        List<ZsyhAfterServiceType> ztrees = new ArrayList<>();
        for (ZsyhAfterServiceType zsyhAfterServiceType : zsyhAfterServiceTypeList)
        {
            if("0".equals(zsyhAfterServiceType.getServiceParentId())){
                ztrees.add(zsyhAfterServiceType);
            }
        }
        for (ZsyhAfterServiceType a : ztrees){
            List<ZsyhAfterServiceType> children = new ArrayList<>();
            for (ZsyhAfterServiceType b : zsyhAfterServiceTypeList)
            {
                if(a.getId().equals(b.getServiceParentId())){
                    children.add(b);
                }
            }
            a.setChildren(children);
        }
        return ztrees;
    }
}
